Use scalar subscript values to select individual elements or subarrays from an array. If the array has more than one dimension, you can specify a scalar subscript for each dimension.
For information on using range expressions to select multiple elements from an array, see Array Subscript Ranges .
For information on using arrays to subscript other arrays, see Using Arrays as Array Subscripts.
For example, to select a single element from a vector of values, use a single scalar subscript value:
array = FINDGEN(5) * !PI ; Create a 5-element array
PRINT, array ; Print the entire array
PRINT, array[3] ; Print the fourth element
IDL Prints:
0.000000 3.14159 6.28319 9.42478 12.5664
9.42478
To select a single element from a multidimensional array, specify one scalar subscript value for each dimension:
mdarray = INDGEN(2,5) ; Create a 2-column by 5-row array
PRINT, mdarray ; Print the entire array
PRINT, mdarray[1,3] ; Print the fourth element of the
; second column
IDL Prints:
0 1
2 3
4 5
6 7
8 9
7
Elements in a multidimensional array can be specified using a single scalar subscript, as if the multidimensional array were in fact a one-dimensional vector:
mdarray = INDGEN(2,5) ; Create a 2-column by 5-row array
PRINT, mdarray[7] ; Print the eighth element of the array
IDL Prints:
7
When subscripting an array using scalar subscript values, you can use negative integers to index the array from the last element of the dimension being indexed. For example, the statement
PRINT, array[-1]
is a simple way to select the last element of array without knowing how many elements array contains. The above statement is equivalent to:
PRINT, array[N_ELEMENTS(array)-1]
Notice that because a scalar can be considered a single element array, subscripting a scalar with -1 (indicating the last element of the array) is equivalent to subscripting the scalar with 0 (indicating the first element of the array). For example:
scalar = 3
PRINT, scalar, scalar[0], scalar[-1]
IDL prints:
3 3 3
You can select an element of a multidimensional array using negative subscript values. For example:
mdstrarr = [['row1_col1', 'row1_col2', 'row1_col3'], $
['row2_col1', 'row2_col2', 'row2_col3'], $
['row3_col1', 'row3_col2', 'row3_col3']]
The following two statements print the same element in mdstrarr:
PRINT, mdstrarr[-3,-2]
PRINT, mdstrarr[0,1]
IDL Prints:
row2_col1
row2_col1
Note: IDL behaves differently when it encounters negative integers in an array being used to subscript another array. See Array Subscripts and Clipping for details.
You cannot subscript a variable that has not yet been defined. Thus, if the variable B has not been previously defined, the statement:
B[0] = 9
will fail with the error “variable is undefined.”